package com.tannm.doan.taskmanager.database;

import java.util.*;

//TaskData class
public class DataRowTask extends DataRow {
	// Table field indexes for field select speedup
	public static class Fid {
		public static final int ID = 0;
		public static final int SUBJECT = 1;
		public static final int DONE = 2;
		public static final int PRIORITY = 3;
		public static final int ALARM = 4;
		public static final int DUEDATE = 5;
	};

	// Table definition
	private final DataField[] tableDef = {
			new DataField(Fid.ID, "_ID", DataField.Type.INT, true, true),
			new DataField(Fid.SUBJECT, "Subject", DataField.Type.TEXT, true,
					false),
			new DataField(Fid.DONE, "Done", DataField.Type.BOOL, true, false),
			new DataField(Fid.PRIORITY, "Priority", DataField.Type.INT, true,
					false),
			new DataField(Fid.ALARM, "Alarm", DataField.Type.BOOL, true, false),
			new DataField(Fid.DUEDATE, "DueDate", DataField.Type.INT, true,
					false), };

	// fields
	private String sSubject = "";
	private boolean bDone = false;
	private int iPriority = 1;
	private boolean bAlarm = false;
	private Calendar calDueDate = Calendar.getInstance();

	// methods
	public DataRowTask(Database userdb) {
		super(userdb);
		setTableDefinition(tableDef);
	}

	// setters
	public void setSubject(String value) {
		sSubject = new String(value.trim());
	}

	public void setDone(boolean value) {
		bDone = value;
	}

	public void setPriority(long value) {
		iPriority = (int) value;
	}

	public void setAlarm(boolean value) {
		bAlarm = value;
	}

	public void setDueDate(Calendar value) {
		if (value == null) {
			calDueDate.setTimeInMillis(0);
		} else {
			calDueDate.setTimeInMillis(value.getTimeInMillis());
			calDueDate.set(Calendar.SECOND, 0);
			calDueDate.set(Calendar.MILLISECOND, 0);
		}
	}

	// getters
	public String getSubject() {
		return sSubject;
	}

	public boolean getDone() {
		return bDone;
	}

	public int getPriority() {
		return iPriority;
	}

	public boolean getAlarm() {
		return bAlarm;
	}

	public boolean usingDueDate() {
		return (calDueDate.getTimeInMillis() > 0);
	}

	public Calendar getDueDate() {
		return calDueDate;
	}

	/*
	 * public String toString() { String s = ""; s += sSubject + "\n"; s +=
	 * String.format("done: %b, priority: %i, alarm: %b", bDone, iPriority,
	 * bAlarm) + "\n"; s += DateFormat.format("EEEE, dd-MM-yyyy, hh:mm",
	 * calDueDate).toString() + "\n"; return s; }
	 */

	@Override
	public boolean validate() {
		if (sSubject.length() > 0) {
			return true;
		}
		return false;
	}

	@Override
	public void setValuesForDataRow() {
		clearContentValues();

		value(Fid.SUBJECT).set(getSubject());
		value(Fid.DONE).set(getDone());
		value(Fid.PRIORITY).set(getPriority());
		value(Fid.ALARM).set(getAlarm());
		value(Fid.DUEDATE).setNull();
		if (usingDueDate())
			value(Fid.DUEDATE).set(getDueDate());
	}

	@Override
	public void getValuesFromDataRow() {
		setSubject(value(Fid.SUBJECT).asString());
		setDone(value(Fid.DONE).asBoolean());
		setPriority(value(Fid.PRIORITY).asLong());
		setAlarm(value(Fid.ALARM).asBoolean());

		if (value(Fid.DUEDATE).isNull()) {
			setDueDate(null);
		} else {
			setDueDate(value(Fid.DUEDATE).asCalendar());
		}
	}

	@Override
	public String getTableName() {
		return Database.sTableNameTasks;
	}

}
